<?php
// 本类由系统自动生成，仅供测试用途
namespace MyClass;
class PUR extends \MyClass\Common {
    public function Index(){	
		$this->v();
    }
    //招标列表接口
	public function Bid(){
		//$city = F('sys_city');
		//print_r($city);exit;
		//招标列表
		$condition = " 1=1";
		if($_REQUEST['city']) {
			$condition.= " AND ProvinceCity = ".$_REQUEST['city'];
			$this->s('cityid',$_REQUEST['city']);
		}
		if($_REQUEST['cityname']){
			$this->s('cityname',$_REQUEST['cityname']);
		}
		if($_REQUEST['order']){
			if($_REQUEST['order'] ==1) {
				$ordertype = 'Pubtime desc';
			} else{
				$ordertype = 'Pubtime asc';
			}
			$this->s("order",$_REQUEST['order']);
		}else{
			$ordertype = 'Pubtime desc';
		}
		if($_REQUEST['st']){
			if($_REQUEST['st'] ==2){//结束
				$condition.= " AND Status = 2";//评标中
			}elseif($_REQUEST['st'] ==4){
				$condition.= " AND  Status = 4";//已结束	
					
			}elseif($_REQUEST['st'] ==1){//采购中
				$condition.= " AND Status = 1";
			}
		}else{
			//$condition.= " AND Status in(1,2,3)";	
			$condition.= " AND Status in(1,2,4)";	
		}
	if(!$_SESSION['UserInfo']['id']){//未登录
		$condition.= " AND Cgtype=1";


	    $list = $this->m('bid')->where($condition)->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->limit(6)->select();

		$this->s('list',$list);
	 }else{
		if(!isset($_SESSION['UserInfo']['cid'])){
				$condition.= " AND Cgtype=1";


		 $list = $this->m('bid')->where($condition)->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->limit(6)->select();	
		}else{	
				$bus = $this->m("yaojia")->where("Syid = ".$_SESSION['UserInfo']['cid'])->select();
			
				foreach($bus as $key=>$vo){
					$bgscid[] = $vo['Bid'];
				}
					//print_r($bgscid);
			
				/*$bus = $this->m('business')->where("Cid = ".$_SESSION['UserInfo']['cid'])->select();//我关注的
				$gus = $this->m('business')->where("Gid = ".$_SESSION['UserInfo']['cid'])->select();//关注我的
				foreach($bus as $key=>$vo){
						foreach($gus as $k=>$v){
							if($vo['Gid'] == $v['Cid']){
								//$bgs[] = $v['Id'];	
								$bgscid[] = $v['Cid'];//获取相互收藏数据的Cid
							}
						}
				}*/
				if($bgscid>0){
					$bgscid = join(",",$bgscid);
				}else{
					$bgscid = 99999999999999;//暂无此数;
				}
				
				$list = $this->m('bid')
					->where($condition." AND ((Id in(".$bgscid.") AND Cgtype = 2) OR (Cid = ".$_SESSION['UserInfo']['cid']." And  Cgtype = 2)  OR  Cgtype =1)")
					->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")
					->order($ordertype)
					->limit(6)
					->select();
			}
			$this->s('list',$list);
		}
		$city = F('sys_city');
		//PRINT_R($city);
		$this->v();
    }
//采购详情
    public function BideTails(){
		if(!isset($_REQUEST['Id'])){
			$this->error("非法操作");
		}
		$Id = $_REQUEST['Id'];
        $info = $this->m('bid')->find($Id);
		$products = unserialize($info['Product']);
		$products = $this->object_array($products);
		if($info['Files']) {
            $array = explode('/',$info['Files'] );
            $file_list = array_filter($array);
        } else {
            $file_list = array();
        }

	
		if($_SESSION['UserInfo']['id']){//是否登陆
			if($_SESSION['UserInfo']['cid']){
				$cid = $_SESSION['UserInfo']['cid'];
				$CompanyName = $this->m("company")->where("Id=".$cid)->find();
				$this->s("CompanyName",$CompanyName);
			}	
		}
		$buy = $this->m('bid')->where(array('Status' => 1 ))->order('Pubtime DESC')->limit(10)->select();
		$this->s('buy',$buy);
		$this->s('file_list',$file_list);
        $this->s('info',$info);
        $this->s('products',$products);
        $this->v();
    }
	  //产品反序列化后转换成数组
    public function object_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        } if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }
	public function BidOrderDo(){
		if(!isset($_SESSION['UserInfo']['cid'])){
			$this->error("请完善信息");
		}
		$Amount ="" ;
		for($i=0;$i<count($_POST['ProductName']);$i++){
			$arry[$i]=array(
				'ProductName' => $_POST['ProductName'][$i],
				'Quantity' => $_POST['Quantity'][$i],
				'Unit' => $_POST['Unit'][$i],
				'Quote' => $_POST['Quote'][$i],
				//'Unitprice' => $_POST['Unitprice'][$i],
				'Industry' => $_POST['Industry'][$i],
				'Specifications' => $_POST['Specifications'][$i],
				'Brand' => $_POST['Brand'][$i],
				'Description' => $_POST['Description'][$i]
			);
			$Amount += $_POST['Quote'][$i]*$_POST['Quantity'][$i];
		}
		
		$condition['Product'] = serialize($arry);
		$data = array(
			'Bid' => $_REQUEST['bid'],
			'Cid' => $_SESSION['UserInfo']['cid'],
			'LinkMan' =>  $_REQUEST['LinkMan'],
			'LinkTel' =>  $_REQUEST['LinkTel'],
			'Product' => $condition['Product'],
			'Time' => time(),
			'Amount' =>$Amount
		);
		$result = $this->m('bid_order')->add($data);
		if($result){
			$this->success("报价成功",T("PUR/Bid"));
		}else{
			$this->error("报价失败");
		}
	
	}
	public function BidAjax(){
		$id = $_POST["id"];
		$cid = $_SESSION['UserInfo']['cid'];
		$list = $this->m("bid_order")->where("Bid=".$id." AND Cid=".$cid )->select();
		if(count($list)>0){//
			echo 1;
		}else{
			$info = $this->m("Bid")->where("Id = ".$id)->find();
			/*查看是公开采购还是邀请采购 如果为邀请采购*/
			if($info['Cgtype'] == 2){//如果为邀请采购
				$yj = $this->m('yaojia')->where("Bid = ".$id." AND Syid = ".$cid)->find();
				if(!$yj){//如果不存在则不属于邀请采购
					echo 2;
				}
			}
		}
		
		
	}
	public function PageAjax(){
		if(!isset($_REQUEST['page'])){
				$this->error("非法操作");
		}
		$thispage = (int)$_REQUEST['page']*6;
		//$_REQUEST['page'] = 1;
	
		$condition = " 1=1";
		if($_REQUEST['city']) {
			$condition.= " AND ProvinceCity = ".$_REQUEST['city'];
		}
		if($_REQUEST['order']){
			if($_REQUEST['order'] ==1) {
				$ordertype = 'Pubtime desc';
			} else{
				$ordertype = 'Pubtime asc';
			}
		}else{
			$ordertype = 'Pubtime desc';
		}
		if($_REQUEST['st']){
			if($_REQUEST['st'] ==3) {//过期
				$condition.= " AND Status = 1";
				$condition.= " AND (UNIX_TIMESTAMP(EndDate)+86400) <=".time();
			}elseif($_REQUEST['st'] ==2){//结束
				$condition.= " AND Status = 2";
			}elseif($_REQUEST['st'] ==1){//采购中
				$condition.= " AND Status = 1";
				$condition.= " AND (UNIX_TIMESTAMP(EndDate)+86400) >".time();
			}
		}else{
			$condition.= " AND Status in(1,2,3)";	
		}
		if(!$_SESSION['UserInfo']['id']){
			$condition.= " AND Cgtype=1";
			$count = $this->m('bid')->where($condition)->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->count();
			$list = $this->m('bid')->where($condition)->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->limit($thispage.", 6")->select();
		}else{
			if(!isset($_SESSION['UserInfo']['cid'])){
				$this->error("请完善公司信息信息");
			}
			$bus = $this->m("yaojia")->where("Syid = ".$_SESSION['UserInfo']['cid'])->select();
		
			foreach($bus as $key=>$vo){
				$bgscid[] = $vo['Bid'];
			}
			if($bgscid>0){
				$bgscid = join(",",$bgscid);
			}else{
				$bgscid = 99999999999999;//暂无此数;
			}
			$count = $this->m('bid')->where($condition." AND ((Id in(".$bgscid.") AND Cgtype = 2) OR (Cid = ".$_SESSION['UserInfo']['cid']." And  Cgtype = 2)  OR  Cgtype =1)")->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")->order($ordertype)->count();
			$list = $this->m('bid')
				->where($condition." AND ((Id in(".$bgscid.") AND Cgtype = 2) OR (Cid = ".$_SESSION['UserInfo']['cid']." And  Cgtype = 2)  OR  Cgtype =1)")
				->field("Id,Title,Address,Co,Pubtime,Status,EndDate,ProvinceCity")
				->order($ordertype)
				->limit($thispage.", 6")
				->select();
		}
		foreach($list as $key=>$v){
			if($v['Status']==1 && (strtotime($v['EndDate'])+86400)>time()){
				$list[$key]['ifjs'] = 1;
			}
			if($v['Status']==2){
				$list[$key]['ifjs'] = 2;
			}
			if($v['Status']==1 && (strtotime($v['EndDate'])+86400)<=time()){
				$list[$key]['ifjs'] = 3;
			}
			$list[$key]['Pubtime'] = date("Y-m-d",$v['Pubtime']);
		}
		
		$ifhy = ceil($count/6);
		if((int)$_REQUEST['page']>$ifhy){
			$list['ifhy'] = 1;
		}
		echo json_encode($list);
	
	}
	
	
}